package com.example.myapplication.activity.live_activity;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.example.myapplication.R;
import com.example.myapplication.utils.DateUtil;

public class ActRequestActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView send;
    private TextView getResponse;
    private ActivityResultLauncher<Intent> register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_request);

        send = findViewById(R.id.tv_send);
        getResponse = findViewById(R.id.tv_receive);
        send.setText("今天疯狂星期四");
        findViewById(R.id.btn_send).setOnClickListener(this);
        //注册回调方法~
        register = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
            if (result != null) {
                Intent intent = result.getData();
                if (intent != null && result.getResultCode() == Activity.RESULT_OK) {
                    Bundle bundle = intent.getExtras();
                    getResponse.setText(bundle.getString("responseContent"));
                    Log.i("MyTAG", "收到响应~" + bundle.getString("responseTime") + bundle.getString("responseContent"));

                }
            }

        });
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(this, ActResponseActivity.class);
        //新建包裹
        Bundle bundle = new Bundle();
        bundle.putString("requestTime", DateUtil.getNowTime());
        bundle.putString("requestContent", send.getText().toString());
        //意图赛包裹
        intent.putExtras(bundle);
        Log.i("MyTAG", "发送消息~" + send.getText());
        register.launch(intent);


    }
}